﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataLibrary;
using Common;

namespace TicketSystem
{
    public partial class UC_RoleRight : UserControl
    {
        public UC_RoleRight()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        private void UC_RoleRight_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            RoleList();
            RoleRightList();
            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid);
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            string name = GetOperator.OperatorName;
            //验证操作员是否可编辑角色和权限
            pm.CanMotifyOperator(this.btn_Add, this.btn_Update,l, name);
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        private void RoleList()
        {
            var role = tse.Roles.Where(u => true).Select(u => new { u.RoleID, u.RoleName }).ToList();
            this.dataGridView1.DataSource = role;
            this.dataGridView1.Columns[0].HeaderText = "角色编号";
            this.dataGridView1.Columns[0].Width = 120;
            this.dataGridView1.Columns[1].HeaderText = "角色名称";
            this.dataGridView1.Columns[1].Width = 200;
            var role1 = tse.Roles.Where(u => true).Select(u => new { u.RoleID, u.RoleName }).ToList();
            this.comRole.DataSource = role1;
            this.comRole.ValueMember = "RoleID";
            this.comRole.DisplayMember = "RoleName";
            //this.comRole.SelectedValue = "RoleID";
        }

        /// <summary>
        /// 角色权限列表
        /// </summary>
        private void RoleRightList()
        {
            int id = Convert.ToInt32(this.comRole.SelectedValue);
            var roleright = tse.RoleRights.Where(u => u.RoieID == id).Select(u => new { u.Rights.RightName }).ToList();
            for (var j = 0; j < roleright.Count; j++)
            {
                if (roleright[j].RightName == "最高权限")
                {
                    this.checkPhone.Checked = true;
                    this.checkPrint.Checked = true;
                    this.checkExport.Checked = true;
                    this.checkUpdate.Checked = true;
                }
                else
                {
                    if (roleright[j].RightName == "显示电话")
                    {
                        this.checkPhone.Checked = true;
                    }
                    if (roleright[j].RightName == "可以打印数据")
                    {
                        this.checkPrint.Checked = true;
                    }
                    if (roleright[j].RightName == "可以导出数据")
                    {
                        this.checkExport.Checked = true;
                    }

                }
            }

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            using (Form_RoleRightAdd ra = new Form_RoleRightAdd())
            {
                ra.ShowDialog();
                //这里调用绑定函数
                LoadData();
            }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            int rows = this.dataGridView1.SelectedRows.Count;
            if (rows > 1)
            {
                MessageBox.Show("一次只能修改一行");
            }
            else if (rows < 1)
            {
                MessageBox.Show("你当前没有选中任何行");
            }
            else
            {
                using (Form_RoleRightUpdate rru = new Form_RoleRightUpdate())
                {
                    rru.RoleId = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value);
                    rru.RoleName = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    rru.ShowDialog();
                    //这里调用绑定函数
                    this.checkPhone.Checked = false;
                    this.checkPrint.Checked = false;
                    this.checkExport.Checked = false;
                    this.checkUpdate.Checked = false;
                    LoadData();
                }

            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            var count = this.dataGridView1.SelectedRows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    int roleid = Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells[0].Value);
                    var role = tse.Roles.Where(u => u.RoleID == roleid);
                    tse.Roles.RemoveRange(role);
                }
                if (tse.SaveChanges() >= 1)
                {
                    for (int i = 0; i < count; i++)
                    {
                        int roleid = Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells[0].Value);
                        var roleright = tse.RoleRights.Where(u => u.RoieID == roleid);
                        tse.RoleRights.RemoveRange(roleright);
                    }
                    if (tse.SaveChanges() >= 1)
                    {
                        MessageBox.Show("删除成功");
                        RoleList();
                    }
                }
            }
            else
            {
                MessageBox.Show("你当前没有选中任何行");
            }
        }

        private void comRole_DropDownClosed(object sender, EventArgs e)
        {
            this.checkPhone.Checked = false;
            this.checkPrint.Checked = false;
            this.checkExport.Checked = false;
            this.checkUpdate.Checked = false;
            RoleRightList();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
